<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\UserLogModel;

class OrderController extends MainController{
    /**
     * 用户下单
     * @queyuan
     * 2016/05/30
     * @param $user_id,$num,$ip
     */
    public function addOrder(){
        $user_id = I("param.user_id");
        $ip = I("param.ip");
        $result = array();
        if(!$user_id || !$ip){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sales = M("cart c")->join("left join one_sales s on (c.sale_id = s.id) left join one_goods g on (s.good_id = g.id)")->where("c.user_id = ".$user_id." and c.status = 0 and s.sale_num < s.need_num and c.num > 0")->order("c.add_time desc")->field("c.id,c.sale_id,c.user_id,c.num,(s.need_num - s.sale_num) as left_num")->select();
            $total_price = M("cart c")->join("left join one_sales s on (c.sale_id = s.id) left join one_goods g on (s.good_id = g.id)")->where("c.user_id = ".$user_id." and c.status = 0 and s.sale_num < s.need_num and c.num > 0")->getField("sum(c.num)");
            if(!$total_price){
                $result['status'] = 1;
                $result['msg'] = "购物车无商品";
            }else{
                do{
                    $order_sn = $this->get_order_sn();
                    break;
                }while($this->check_order_sn($order_sn) > 0);
                $data = array(
                    'order_sn'=>$order_sn,
                    'user_id'=>$user_id,
                    'total_price'=>$total_price,
                    'order_time'=>time(),
                    'num'=>$total_price,
                );
                $res = M("order")->add($data);
                if(!$res){
                    $result['status'] = 1;
                    $result['msg'] = "下单失败";
                }else{
                    foreach ($sales as $sale) {
                        $sale_data = array(
                            'sale_id'=>$sale['sale_id'],
                            'order_id'=>$res,
                            'num'=>$sale['num'],
                        );
                        $res2 = M("order_sales")->add($sale_data);
                        if($res2){
                            M("cart")->where("id = ".$sale['id'])->save(array('status'=>1));
                        }
                    }
                    $result['status'] = 0;
                    $result['msg'] = "下单成功";
                    $result['order_id'] = $res;
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 生成订单号
     * @queyuan
     * 2016/05/30
     */
    function get_order_sn() {
        return date('YmdHis').rand(1000,9999);
    }

    /**
     * 检测订单号是否可用
     * @queyuan
     * 2016/05/30
     * @param $order_sn
     */
    function check_order_sn($order_sn) {
        return M('order')->where("order_sn = ".$order_sn)->count();
    }

    /**
     * 用户订单列表
     * @queyuan
     * 2016/05/30
     * @param $user_id,$flag:0-未支付 1-已支付,$page
     */
    public function userOrder(){
        $user_id = I("param.user_id");
        $flag = I("param.flag",0,'int');
        $page = I("param.page",0,'int');
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['status'] = "参数错误";
        }else{
            $where = "user_id = ".$user_id;
            $where .= " and status = ".$flag;
            $count = M("order")->where($where)->count();
            $num = ceil($count / C('pagesize'));
            if($page < 0){
                $page = 0;
            }
            if($page >= $num){
                $page = $num - 1;
            }
            $limit = C("pagesize") * $page;
            $list = M("order")->where($where)->field("id,order_sn,total_price,num,order_time,status")->order("pay_time desc,order_time desc")->limit($limit,C('pagesize'))->select();
            if($list){
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['list'] = $list;
                $result['page'] = $page;
                $result['num'] = $num;
            }else{
                $result['status'] = 1;
                $result['msg'] = "无数据";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 订单详情
     * @queyuan
     * 2016/05/30
     * @param $id
     */
    public function orderDetail(){
        $id = I("param.id");
        $result = array();
        if(!$id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $detail = M("order")->where("id = ".$id)->field("id,order_sn,total_price,status,num,order_time,pay_time,pay_type,address,phone,real_name")->find();
            $detail['sales'] = M("order_sales o")->join("left join one_sales s on (o.sale_id = s.id) left join one_goods g on (s.good_id = g.id)")->where("o.order_id = ".$id." and o.num != 0")->field("o.sale_id,o.num,g.name,g.index_img,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num")->select();
            if(!$detail){
                $result['status'] = 1;
                $result['msg'] = "无此订单";
            }else{
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['detail'] = $detail;
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 修改订单余额使用情况
     * @queyuan
     * 2016/07/13
     * @param $order_id,$money
     */
    public function moneyPaySave(){
        $order_id = I("param.order_id");
        $money = I("param.money");
        $result = array();
        if(!$order_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            if(!$money){
                M("order")->where("id = ".$order_id)->save(array('use_money'=>0));
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['left'] = 0;
            }else{
                $order = M("order")->where("id = ".$order_id)->field("user_id,total_price")->find();
                if(!$order){
                    $result['status'] = 1;
                    $result['msg'] = "无此订单";
                }
                if($order['total_price']){
                    $user_money = M("user")->where("id = ".$order['user_id'])->getField("money");
                    if($user_money >= $order['total_price']){
                        M("order")->where("id = ".$order_id)->save(array('use_money'=>$total_price));
                        $result['status'] = 0;
                        $result['msg'] = "请求成功";
                        $result['left'] = 0;
                    }else{
                        M("order")->where("id = ".$order_id)->save(array('use_money'=>$user_money));
                        $left = $order['total_price'] - $user_money;
                        $result['status'] = 0;
                        $result['msg'] = "请求成功";
                        $result['left'] = $left;
                    }
                }else{
                    $result['status'] = 1;
                    $result['msg'] = "订单已取消";
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 余额支付完成
     * @queyuan
     * 2016/07/13
     * @param $order_id
     */
    public function moneyPayComplete(){
        $order_id = I("param.order_id");
        $result = array();
        if(!$order_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $pay_time = time();
            $pay_time_ss = round((explode(" ",microtime())[0]*1000),0);
            $res = M("order")->where("id = ".$order_id)->save(array('pay_time'=>$pay_time,'pay_time_ss'=>$pay_time_ss,'status'=>1,'pay_time_str'=>(date("His",$pay_time).$pay_time_ss)));
            if(!$res){
                $result['status'] = 1;
                $result['msg'] = "请求失败";
            }else{
                $order = M("order")->where("id = ".$order_id)->field("user_id,use_money")->find();
                $money = M("user")->where("id = ".$order['user_id'])->getField("money");
                M("user")->where("id = ".$order['user_id'])->save(array('money'=>($money - $order['use_money'])));
                $sales = M("order_sales")->where("order_id = ".$order_id." and num != 0")->select();
                foreach($sales as $sale) {
                    $sale_detail = M("sales")->where("id = ".$sale['sale_id'])->field("need_num,sale_num")->find();
                    $sale_num = $sale_detail['sale_num'];
                    $need_num = $sale_detail['need_num'];
                    M("sales")->where("id = ".$sale['sale_id'])->save(array('sale_num'=>($sale_num+$sale['num'])));
                    $sale_num = $sale_num + $sale['num'];
                    for ($i=0; $i < $sale['num']; $i++) { 
                        $code = rand(1,$need_num)+10000000;
                        while($this->checkOrderCode($code,$sale['sale_id']) > 0){
                            $code = rand(1,$need_num)+10000000;
                        }
                        $data = array(
                            'order_id'=>$order_id,
                            'code'=>$code,
                            'status'=>1,
                            'sale_id'=>$sale['sale_id'],
                        );
                        M("order_code")->add($data);
                    }
                    if($sale_num == $need_num){
                        $sum = M("order o")->join("left join one_order_sales s on (s.order_id = o.id)")->where("s.sale_id = ".$sale['sale_id']." and status = 1")->order("o.pay_time desc")->limit(0,50)->getField("sum(o.pay_time_str))");
                        $re = ($sum % $need_num) + 10000001;
                        $code_id = M("order_code")->where("code = '".$re."' and sale_id = ".$sale['sale_id'])->getField("id");
                        $user = M("user u")->join("left join one_order o on (o.user_id = u.id) left join one_order_code c on (c.order_id = o.id)")->where("c.id = ".$code_id)->field("u.phone,u.name")->find();
                        $good_name = M("goods g")->join("left join one_sales s o (s.good_id = g.id)")->where("s.id = ".$sale['sale_id'])->getField("g.name as good_name");
                        $r = $this->sendCode($user['phone'],$user['name'],$good_name);
                        M("sales")->where("id = ".$sale['sale_id'])->save(array("code_id"=>$code_id,'win_time'=>(time() + (15 * 60))));
                    }
                }
                $result['status'] = 0;
                $result['msg'] = "支付成功";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 检测订单号是否可用
     * @queyuan
     * 2016/05/30
     * @param $order_sn
     */
    function checkOrderCode($code,$sale_id) {
        return M('order_code')->where("code = '".$code."' and sale_id = ".$sale_id)->count();
    }

    /**
     * 生成充值订单
     * @queyuan
     * 2016/07/14
     * @param $user_id,$price
     */
    public function addRechargeOrder(){
        $user_id = I("param.user_id");
        $price = I("param.price");
        $result = array();
        if(!$user_id || !$price){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $data = array(
                'user_id'=>$user_id,
                'total_price'=>$price,
                'order_time'=>time(),
            );
            do{
                $order_sn = $this->getRechargeOrderSn();
                break;
            }while($this->checkRechargeOrderSn($order_sn) > 0);
            $data['order_sn'] = $order_sn;
            $res = M("order_recharge")->add($data);
            if($res){
                $data['id'] = $res;
                $result['status'] = 0;
                $result['msg'] = "生成成功";
                $result['order'] = $data;
            }else{
                $result['status'] = 1;
                $result['msg'] = "生成失败";
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 生成充值订单号
     * @queyuan
     * 2016/05/30
     */
    function getRechargeOrderSn() {
        return date('YmdHis').rand(1000,9999);
    }

    /**
     * 检测充值订单号是否可用
     * @queyuan
     * 2016/05/30
     * @param $order_sn
     */
    function checkRechargeOrderSn($order_sn) {
        return M('order_recharge')->where("order_sn = ".$order_sn)->count();
    }

    /**
     * 发送获奖短信
     * @queyuan
     * 2016/05/31
     * @param $tel,$username,$good_name
     */
    public function sendCode() {
        $tel = I("param.tel");
        $username = I("param.username");
        $good_name = I("param.good_name");
        $sms = send_sale_msg($tel,$username,$good_name);
        if($sms == '00'){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 检查订单中是否有商品库存不够
     * @queyuan
     * 2016/07/14
     * @param $order_id
     */
    public function checkOrderSales(){
        $order_id = I("param.order_id");
        $result = array();
        if(!$order_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sales = M("order_sales o")->join("left join one_sales s on (o.sale_id = s.id)")->where("o.order_id = ".$order_id)->field("o.id,o.num,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num")->select();
            foreach ($sales as $sale) {
                if($sale['num'] > $sale['left_num']){
                    M("order_sales")->where("id = ".$sale['id'])->save(array('num'=>$sale['left_num']));
                }
            }
            $total_price = M("order_sales")->where("order_id = ".$order_id)->getField('sum(num)');
            M("order")->where("id = ".$order_id)->save(array('total_price'=>$total_price,'num'=>$total_price));
            $result['status'] = 0;
            $result['msg'] = "请求成功";
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获奖记录
     * @queuyan
     * 2016/07/14
     * @param $user_id
     */
    public function winList(){
        $user_id = I("param.user_id");
        $result = array();
        if(!$user_id){
            $result['status'] = 1;
            $result['msg'] = "数据错误";
        }else{
            $list = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_goods g on (s.good_id = g.id)")->where("o.user_id = ".$user_id)->field("s.id as sale_id,g.name as good_name,c.code,g.index_img,o.pay_time,s.win_time")->order("s.win_time desc")->select();
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获奖详情
     * @queyuan
     * 2016/07/14
     * @param $sale_id
     */
    public function winDetail(){
        $sale_id = I("param.sale_id");
        $result = array();
        if(!$sale_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $detail = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_goods g on (s.good_id = g.id) left join one_user u on (o.user_id = u.id)")->where("s.id = ".$sale_id)->field("s.id as sale_id,g.name as good_name,g.img,c.code,o.pay_time,s.win_time,o.id as order_id,u.name,u.avatar,o.order_sn")->find();
            if(!$detail){
                $result['status'] = 1;
                $result['msg'] = "无此获奖记录";
            }else{
                $detail['codes'] = M("order_code")->where("sale_id = ".$sale_id." and order_id = ".$detail['order_id'])->order("code asc")->select();
                $detail['img'] = explode(",",$detail['img']);
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['detail'] = $detail;
            }
        }
        echo json_encode($result);
        exit;
    }
}
?>